﻿namespace Mighty.Specs

import System
import System.IO
import System.Collections
import System.Collections.Generic
import Specter.Framework
import Mighty.Lib.Document

// file loading functionality for Mighty.. which is a misnomer,
// since the "filesystem loading" is actually stream based
context "Default filesystem loading":
	// item under test
	loadProvider as IOpenProvider
	
	// dependencies
	loadParams as Dictionary[of string, object]
	mockFile as List of string
	mockFileStream as MemoryStream
	
	setup:
		// background legwork
		loadProvider = DefaultOpenHandler()

		// used for test verification
		mockFile = List of string()
		mockFile.Add("this is line 1")
		mockFile.Add("this is line 2")
				
		// DefaultOpenHandler deps
		
		// needed by open provider ... reads from this stream
		// (is FileStream in production)
		mockFileStream = MemoryStream()
		writer as StreamWriter = StreamWriter(mockFileStream)
		for i in mockFile:
			writer.WriteLine(i)
		writer.Flush()
		// little known fact... best seek your ass back to the beginning if you wanna use
		// this fucker
		mockFileStream.Seek(0,SeekOrigin.Begin)
		
		// needed by open provider ... is how parameters are
		// passed into the provider interface
		loadParams = Dictionary[of string, object]()
		
	
	// As an app user
	specify "I want to be able to load a file from a stream or filesystem":
		// so that I can edit an existing file
		
		// given
		// loadProvider
		// mockFileStream
		// loadParams
		loadParams.Add("Stream",cast(object,mockFileStream))
		
		
		// when
		// a file is loaded
		actual as List of string = loadProvider.Open(loadParams)
		
		mockFile.Count.Must == 2
		actual.Count.Must == 2
		
		// then
		for i in range(0,actual.Count-1):
			// also, do some trimmin' if you know what's good for you
			actual[i].Trim().Must == mockFile[i].Trim()